(function () {
  function sleep(time) {
    return new Promise((resolve) => setTimeout(resolve, time));
  }

  const go = async () => {
    console.log('开始抢票');
    // 表示抢 N + 1个时间档次
    let ticketTime = 2
    const byTimes = [1, 0]
    // 表示抢第N + 1个票档
    let ticketPd = 5
    // 三个备用票档
    const byPds = [4, 3, 2, 1, 0]

    // 表示买几张票 
    const count = 2
    const intervalTime = 80

    const domClick = (classname, index, callback, dom) => {
      let startDom = document
      if (dom) {
        startDom = dom
      }
      const bugBtns = startDom.getElementsByClassName(classname)
      if (bugBtns && bugBtns.length > 0) {
        // 点击购票按钮
        bugBtns[index].click()
      }

      callback && callback()
    }

    const hasDom = (classname, id) => {
      if (classname) {
        const doms = document.getElementsByClassName(classname)
        return doms && doms.length
      }
      if (id) {
        const doms = document.getElementById(id)
        return Boolean(doms)
      }
    }

    // page1
    // 获取购票dom，并且点击
    console.log('// 获取购票dom，并且点击 1');
    domClick('detail-normal-button', 0)
    // 等待一下
    const interval2 = setInterval(() => {
      if (hasDom('show-list-wrap')) {
        clearInterval(interval2)
        step2()
      }
    }, intervalTime)

    const step2 = () => {
      // 获取并且；点击选择时间
      console.log('// 获取并且；点击选择时间 2');
      const boxOne = document.getElementsByClassName('show-list-wrap')?.[0]
      const tTimeOptions = boxOne?.getElementsByClassName('wrap-item')
      const timeSelected = boxOne.getElementsByClassName('wrap-item selected')
      const hasTime = tTimeOptions?.[ticketTime]
      // 如果当前的时间下标的选项没了，
      if (!hasTime
        || hasTime.className.indexOf('outOfStock') !== -1
        || hasTime.className.indexOf('soldOut') !== -1) {
        // 去票档里面找，备用的票
        for (const timeIndex of byTimes) {
          if (tTimeOptions?.[timeIndex]
            && tTimeOptions?.[timeIndex].className.indexOf('outOfStock') === -1
            && tTimeOptions?.[timeIndex].className.indexOf('outOfStock') === -1) {
            ticketTime = timeIndex
            break
          }
        }
      }
      // 如果没有选中的，或者当前想选择的不是这个select的，就需要手动点击一下；
      if (!timeSelected?.[0] || tTimeOptions?.[ticketTime].className !== timeSelected?.[0].className) {
        domClick('wrap-item', ticketTime, undefined, boxOne)
      }

      const interval3 = setInterval(() => {
        if (hasDom(false, 'ticket-list-wrap')) {
          clearInterval(interval3)
          step3()
        }
      }, intervalTime)
    }

    const step3 = () => {
      // 获取票档并且点击
      console.log('// 获取票档并且点击 3');
      const boxTwo = document.getElementById('ticket-list-wrap')
      const pdOptions = boxTwo.getElementsByClassName('wrap-item')
      const hasPD = pdOptions[ticketPd]
      // 想要的票档被抢了，应该去换一个票档
      if (!hasPD
        || hasPD.className.indexOf('outOfStock') !== -1
        || hasPD.className.indexOf('soldOut') !== -1
      ) {
        // 去票档里面找，备用的票
        for (const pdIndex of byPds) {
          if (pdOptions?.[pdIndex]
            && pdOptions?.[pdIndex].className.indexOf('outOfStock') === -1
            && pdOptions?.[pdIndex].className.indexOf('soldOut') === -1) {
            ticketPd = pdIndex
            break
          }
        }
      }
      domClick('wrap-item', ticketPd, undefined, boxTwo)

      const interval4 = setInterval(() => {
        if (hasDom('ticket-number-select-add', false)) {
          clearInterval(interval4)
          step4()
        }
      }, intervalTime)
    }

    const step4 = async () => {
      // 获取加号按钮并点击
      console.log('// 获取加号按钮并点击 4');
      domClick('ticket-number-select-add', 0)

      // 如果是两份，就点击确认
      const nums = document.getElementsByClassName('ticket-number-select-amount')
      await sleep(80)
      if (nums && nums.length > 0) {
        if (nums[0].innerText === `${count}份`) {
          // 点击确定
          // 获取加号按钮并点击
          console.log('// 点击完成按钮 5');
          domClick('button', 0)
        }
      }
    }
    clearInterval(interval)
  }

  const interval = setInterval(() => {
    const buyBtns = document.getElementsByClassName('detail-normal-button')
    if (buyBtns?.length > 0) {
      console.log('开始抢票');
      clearInterval(interval)
      go()
    } else {
      console.log('没到时间');
      return
    }
  }, 80);
})()